﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;

namespace PeisongWeb.admin
{
    public partial class myCustomerEdit : adminPage
    {
        protected int companyId
        {
            get
            {
                int tmp;
                try { tmp = Convert.ToInt32(ViewState["companyId"]); }
                catch { tmp = 0; }
                return tmp;
            }
            set
            { ViewState["companyId"] = value; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (hasPermission("IB", 1))
                {
                    if (Request.QueryString["company"] != null)
                    {
                        if (Library.Number.IsIntPositive(Request.QueryString["company"]))
                        {
                            companyId = Convert.ToInt32(Request.QueryString["company"]);
                        }
                    }
                    drpBind();
                    hid_initScore.Value = logic.webset.list().CompanyScore.ToString();
                    if (companyId != 0)
                    {
                        showType.Visible = false;
                        showType2.Visible = true;
                        buyerType.Visible = true;


                        getDetails();
                    }
                    else
                    {
                        showType.Visible = true;
                        showType2.Visible = false;
                    }
                }
                else
                {
                    Library.Script.ClientMsgUrl("没有权限！", "index.aspx");
                    return;
                }
            }
        }
        private void drpBind()
        {
            ddl_Area.DataSource = logic.sysCode.areaList();
            ddl_Area.DataBind();
            ddl_Area.Items.Insert(0, new System.Web.UI.WebControls.ListItem("--请选择地区--", ""));

            ddl_receiveTime.DataSource = logic.sysCode.receiveTime();
            ddl_receiveTime.DataBind();
            ddl_receiveTime.Items.Insert(0, new System.Web.UI.WebControls.ListItem("--请选择送货时间--", ""));

            ddl_PayMethod.DataSource = logic.sysCode.payMethodList();
            ddl_PayMethod.DataBind();
            ddl_PayMethod.Items.Insert(0, new System.Web.UI.WebControls.ListItem("--请选择结帐方式--", ""));

            rpt_type.DataSource = logic.company.getType(companyId);
            rpt_type.DataBind();


            rpt_CategoryRoot.DataSource = logic.company.getBuyRange(companyId);
            rpt_CategoryRoot.DataBind();

            rpt_buyerType.DataSource = logic.company.getBuyerTypeList(companyId);
            rpt_buyerType.DataBind();

            ddl_SalesPerson.DataSource = logic.sysAdmin.listWithSalesperson();
            ddl_SalesPerson.DataBind();
            ddl_SalesPerson.Items.Insert(0, new System.Web.UI.WebControls.ListItem("--请选择销售员--", "0"));
        }
        private void getDetails()
        {
            CompanyEntity cp = logic.company.getById(companyId);
            txt_CompanyName.Text = cp.Company;

            txt_Address.Text = cp.Address;

            ddl_Area.SelectedIndex = ddl_Area.Items.IndexOf(ddl_Area.Items.FindByValue(cp.AreaID.ToString()));
            txt_CompanyNo.Text = cp.CompanyNo;
            txt_Contact.Text = cp.Contact;
            txt_Email.Text = cp.Email;
            txt_eatPerson.Text = cp.EatPerson;
            txt_Fax.Text = cp.Fax;
            txt_IDCard.Text = cp.IdCard;
            chx_isCheck.Checked = cp.IsCheck;
            txt_Location.Text = cp.Location;
            txt_Mobile.Text = cp.Mobile;
            ddl_PayMethod.SelectedIndex = ddl_PayMethod.Items.IndexOf(ddl_PayMethod.Items.FindByValue(cp.PayMethod));
            txt_Phone.Text = cp.Phone;
            txt_PostCode.Text = cp.Postcode;
            txt_Suggest.Text = cp.Suggest;
            ddl_SalesPerson.SelectedIndex = ddl_SalesPerson.Items.IndexOf(ddl_SalesPerson.Items.FindByValue(cp.salesperson));
            ddl_receiveTime.SelectedIndex = ddl_receiveTime.Items.IndexOf(ddl_receiveTime.Items.FindByValue(cp.ReceiveTime));

            chkWeeklyPrice.Checked = cp.EnabledWeeklyPrice;

            hd_type.Value = cp.Type;
            hd_buyType.Value = cp.buyerType;
            lbl_score.Text = cp.Score.ToString();
            switch (cp.Type)
            {
                case "1":
                    lbl_type.Text = "供应商";
                    break;
                case "2":
                    lbl_type.Text = "采购商";
                    break;
                case "3":
                    lbl_type.Text = "物流商";
                    break;
            }
            if (cp.Type == "1")
            {
                if (cp.cooperateType == "2")
                {
                    txt_cooperatePercent.Text = string.Format("{0:F2}", cp.cooperatePercent);
                    chk_CooperateType2.Checked = true;
                }
                else
                    chk_CooperateType1.Checked = true;
                ScriptManager.RegisterStartupScript(this, this.GetType(), "key", "showSupplyInfor(" + cp.cooperateType + ");", true);
            }
            else if (cp.Type == "2")
            {
                buyerType.Visible = true;
                txtAutoPercent.Text = string.Format("{0:F2}", cp.autoPricePercent);
                ScriptManager.RegisterStartupScript(this, this.GetType(), "key", "showCustomerInfor();", true);
            }


        }

        protected void btn_Save_Click(object sender, EventArgs e)
        {
            if (companyId == 0)
            {
                if (logic.company.isExistCompanyNo(txt_CompanyNo.Text.Trim()))
                {
                    Library.Script.ClientMsg("企业编号已存在，请重新输入！");
                    return;
                }
            }
            CompanyEntity cp = new CompanyEntity();

            cp.Company = txt_CompanyName.Text;
            cp.VipType = "1";
            cp.Soruce = "1";
            cp.Address = txt_Address.Text;
            cp.AdminId = logic.sysAdmin.AdminID;
            cp.AreaID = Convert.ToInt32(ddl_Area.SelectedValue);
            cp.CompanyNo = txt_CompanyNo.Text;
            cp.Contact = txt_Contact.Text;
            cp.Email = txt_Email.Text;
            cp.Fax = txt_Fax.Text;
            cp.IdCard = txt_IDCard.Text;
            cp.IsCheck = chx_isCheck.Checked;
            cp.Location = txt_Location.Text;
            cp.Mobile = txt_Mobile.Text;
            cp.PayMethod = ddl_PayMethod.SelectedValue;
            cp.Phone = txt_Phone.Text;
            cp.Postcode = txt_PostCode.Text;
            cp.Suggest = txt_Suggest.Text;
            cp.Type = hd_type.Value;
            cp.buyRange = hd_buyRange.Value;
            cp.buyerType = hd_buyType.Value;
            cp.salesperson = ddl_SalesPerson.SelectedValue;
            cp.EatPerson = txt_eatPerson.Text.Trim();

            cp.EnabledWeeklyPrice = chkWeeklyPrice.Checked;
            cp.ReceiveTime = ddl_receiveTime.SelectedValue;

            if (cp.Type == "1")
            {
                cp.cooperateType = hd_cooperateType.Value;
                cp.ReceiveTime = "7:00";
                if (cp.cooperateType == "2")
                {
                    cp.cooperatePercent = Convert.ToDecimal(txt_cooperatePercent.Text);
                }
            }
            if (cp.Type == "2")
            {
                cp.autoPricePercent = Convert.ToDecimal(txtAutoPercent.Text);
            }



            if (companyId != 0)
            {
                cp.CompanyId = companyId;
                cp.Score = Convert.ToInt32(lbl_score.Text.Trim());
                logic.company.update(cp);
                logic.company.updateByCheck(cp);
            }
            else
            {
                cp.Score = cp.Type == "1" ? Convert.ToInt32(hid_initScore.Value.Trim()) : 0;
                cp.CompanyId = logic.company.insert(cp);
                logic.company.updateByCheck(cp);
            }
            Library.Script.ClientMsgUrl("保存成功！", "myCustomerList.aspx");

        }
    }
}
